package com.e3.basdoc.employee.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.employee.client.EmployeeRPCService;
import com.e3.basdoc.employee.model.EmployeeModel;
import com.e3.basdoc.employee.model.EmployeeQC;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;

public class EmployeeRPCServiceImpl extends GwtRPCAction implements	EmployeeRPCService {
		
	public EmployeeModel newEmployee(final EmployeeModel model) throws WebException {
		return new GwtRPCExecutor<EmployeeModel>() {
			@Override
			public EmployeeModel execute() throws ServiceException, Exception {
				return EmployeeServiceClient.getEmployeeService().newEmployee(model);
			}
		}.doExecute();
	}

	public EmployeeModel modifyEmployee(final EmployeeModel model) throws WebException {
		return new GwtRPCExecutor<EmployeeModel>() {
			@Override
			public EmployeeModel execute() throws ServiceException, Exception {
				return EmployeeServiceClient.getEmployeeService().modifyEmployee(model);
			}
		}.doExecute();
	}

	public List<EmployeeModel> getEmployeeByCondition(final EmployeeQC goodsQC) throws WebException {
		return new GwtRPCExecutor<List<EmployeeModel>>() {
			@Override
			public List<EmployeeModel> execute() throws ServiceException, Exception {
				List<EmployeeModel> models = EmployeeServiceClient.getEmployeeQueryService().getEmployeeByCondition(goodsQC);
				fillDept(models);
				fillBank(models);
				fillClasses(models);
				return models;
			}
		}.doExecute();
	}

	protected void fillClasses(List<EmployeeModel> models) {
		if(models==null||models.isEmpty()){
			return;
		}
	}

	protected void fillBank(List<EmployeeModel> models) {
		if(models==null||models.isEmpty()){
			return;
		}
	}

	protected void fillDept(List<EmployeeModel> models) {
		if(models==null||models.isEmpty()){
			return;
		}
	}

	public void deleteEmployees(List<EmployeeModel> models) throws WebException {
		if(models == null||models.isEmpty()){
			return;
		}
		final List<Long> ids = new ArrayList<Long>(models.size());
		for (EmployeeModel goodsModel : models) {
			ids.add(goodsModel.getId());
		}
		
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				EmployeeServiceClient.getEmployeeService().deleteEmployeeByIds(ids);
			}
		}.doExecute();
	}

	public PagingLoadResult<EmployeeModel> getPagingEmployee(final PagingLoadConfig config)	throws WebException {
		final EmployeeQC qc = config.get("LOAD_CONDTION");
		List<EmployeeModel> models = new GwtRPCExecutor<List<EmployeeModel>>() {
			int start = config.getOffset();
			int max = config.getLimit();
			@Override
			public List<EmployeeModel> execute() throws ServiceException,	Exception {
				List<EmployeeModel> goods = EmployeeServiceClient.getEmployeeQueryService().getPagingEmployee(qc, start, max);
				if (goods == null || goods.isEmpty()) {
					return goods;
				}
				return goods;
			}
			
		}.doExecute();
		if(models == null||models.isEmpty()){
			return new BasePagingLoadResult<EmployeeModel>(new ArrayList<EmployeeModel>());
		}
		
		long size = new GwtRPCExecutor<Long>(){
			@Override
			public Long execute() throws ServiceException, Exception {
				return EmployeeServiceClient.getEmployeeQueryService().getEmployeeSizeByCondition(qc);
			}
			
		}.doExecute();
		
		return new BasePagingLoadResult<EmployeeModel>(models, config.getOffset(), (int)size);
	}

}
